﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingDescriptorBase.cs" company="Radical Research Ltd">
//   Copyright (c) Radical Research Ltd. The MIT License (MIT)
// </copyright>
// <summary>
//   Descriptor for a setting
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RadicalResearch.MiniState.Descriptors
{
    using System.Diagnostics.CodeAnalysis;

    /// <summary>Descriptor for a setting</summary>
    public abstract class SettingDescriptorBase
    {
        #region Constants and Fields

        /// <summary>
        ///   default value key prefix
        /// </summary>
        private const string DefaultValueKeyPrefix = "0";

        /// <summary>
        ///   is the setting included in hash validation
        /// </summary>
        private readonly bool isValidated;

        /// <summary>
        ///   key of the value in the collection
        /// </summary>
        private readonly string valueKey;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="SettingDescriptorBase"/> class.</summary>
        /// <param name="valueKey">The valueKey used to identify the value in the value collection.</param>
        /// <param name="isValidated">if set to <c>true</c> value is validated with hash.</param>
        protected SettingDescriptorBase(string valueKey, bool isValidated)
        {
            this.valueKey = valueKey;
            this.isValidated = isValidated;
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Indicates whether the setting validated.
        /// </summary>
        /// <value><c>true</c> if this setting is validated; otherwise, <c>false</c>.</value>
        [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", 
            "SA1623:PropertySummaryDocumentationMustMatchAccessors", Justification = "Reviewed.")]
        public bool IsValidated
        {
            get
            {
                return this.isValidated;
            }
        }

        /// <summary>
        ///   Gets valueKey used to identify the value in the values.
        /// </summary>
        public string ValueKey
        {
            get
            {
                return string.Concat(this.ValueKeyPrefix, this.valueKey);
            }
        }

        /// <summary>
        ///   Gets the values key prefix.
        /// </summary>
        protected virtual string ValueKeyPrefix
        {
            get
            {
                return DefaultValueKeyPrefix;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>Reads the value.</summary>
        /// <param name="values">The values.</param>
        /// <returns>value as an object</returns>
        public abstract object ReadValue(IValueCollection values);

        /// <summary>Writes the value.</summary>
        /// <param name="values">The values.</param>
        /// <param name="value">The value.</param>
        public abstract void WriteValue(IValueCollection values, object value);

        #endregion

        #region Methods

        /// <summary>Transforms the read.</summary>
        /// <param name="value">The value.</param>
        /// <returns>Transformed string.</returns>
        protected virtual string TransformRead(string value)
        {
            return value;
        }

        /// <summary>Transforms the write.</summary>
        /// <param name="value">The value.</param>
        /// <returns>Transformed string.</returns>
        protected virtual string TransformWrite(string value)
        {
            return value;
        }

        #endregion
    }
}